package dao

import (
	"errors"
	"go-bluebell/model"

	"go.uber.org/zap"

	"gorm.io/gorm"
)

func GetCommunityList() (communityList []*model.Community, err error) {
	err = model.DB.Find(&communityList).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		zap.L().Warn("数据库中没有社区信息")
		err = nil
	}
	return
}

func GetCommunityDetailByID(communityID int64) (community *model.CommunityDetail, err error) {
	community = new(model.CommunityDetail)
	err = model.DB.Table("communities").First(&community, communityID).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			err = model.ErrorInvalidID
		}
	}
	return
}
